package org.androidpn.server.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.androidpn.server.util.XMLUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * entity 好友消息
 * 
 * @author 蔡琪
 * 
 */
@Entity
@Table(name = "tk_friend_message")
public class FriendMessage extends BaseEntity {

	private static final long serialVersionUID = 8716983378303312867L;

	/**
	 * 好友消息类型
	 * 
	 * @author 蔡琪
	 * 
	 */
	public enum FriendMessageType {
		/** 文本 */
		text,
		/** mp3 */
		mp3
	}

	/** 好友消息状态 */
	public enum FriendMessageStatus {
		/** 待发 */
		toSend,
		/** 发送中 */
		sending,
		/** 已发送 */
		send,
		/** 已读 */
		hasRead
	}

	/** 好友消息类型 */
	@Column(name = "type", nullable = false)
	private FriendMessageType friendMessageType;

	/** 好友消息状态 */
	@Column(name = "status", nullable = false)
	private FriendMessageStatus friendMessageStatus;

	/** 消息内容 */
	@Column(name = "content", length = 1024)
	private String content;

	/** 发送者 */
	@ManyToOne(fetch = FetchType.EAGER)
	private Role sender;

	/** 接收者 */
	@ManyToOne(fetch = FetchType.EAGER)
	private Role receiver;

	public FriendMessageType getFriendMessageType() {
		return friendMessageType;
	}

	public void setFriendMessageType(FriendMessageType friendMessageType) {
		this.friendMessageType = friendMessageType;
	}

	public FriendMessageStatus getFriendMessageStatus() {
		return friendMessageStatus;
	}

	public void setFriendMessageStatus(FriendMessageStatus friendMessageStatus) {
		this.friendMessageStatus = friendMessageStatus;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Role getSender() {
		return sender;
	}

	public void setSender(Role sender) {
		this.sender = sender;
	}

	public Role getReceiver() {
		return receiver;
	}

	public void setReceiver(Role receiver) {
		this.receiver = receiver;
	}

	/**
	 * 从xml字符串中解析出Friend
	 * 
	 * @param xml
	 *            xml字符串
	 * @return Room对象
	 */
	@Transient
	public static FriendMessage fromXML(String xml) {
		FriendMessage friendMessage = BaseEntity.fromXML(xml,
				FriendMessage.class);
		Document doc;
		try {
			doc = DocumentHelper.parseText(xml);
			Element element = doc.getRootElement();
			// 组装Friend信息
			String friendMessageType = element.elementText("friendMessageType");
			String friendMessageStatus = element
					.elementText("friendMessageStatus");
			String content = element.elementText("content");
			/** 角色信息 */
			Role sender = null;
			Element senderElement = element.element("sender");
			if (senderElement != null) {
				Element roleElement = senderElement.element("role");
				sender = Role.fromXML(roleElement.asXML());
			}
			Role receiver = null;
			Element receiverElement = element.element("receiver");
			if (receiverElement != null) {
				Element roleElement = receiverElement.element("role");
				receiver = Role.fromXML(roleElement.asXML());
			}
			friendMessage.setFriendMessageType(XMLUtils
					.getFriendMessageTypFromStr(friendMessageType));
			friendMessage.setFriendMessageStatus(XMLUtils
					.getFriendMessageStatus(friendMessageStatus));
			friendMessage.setContent(content);
			friendMessage.setSender(sender);
			friendMessage.setReceiver(receiver);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return friendMessage;
	}

	/**
	 * 将当前对象转化为xml字符串
	 * 
	 * @return
	 */
	@Transient
	public String toXML() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<friendMessage>");
		buffer.append(super.toXML());
		buffer.append(XMLUtils.createSingleNode("friendMessageType",
				this.friendMessageType));
		buffer.append(XMLUtils.createSingleNode("friendMessageStatus",
				this.friendMessageStatus));
		buffer.append(XMLUtils.createSingleNode("content", this.content));
		if (sender != null) {
			buffer.append(XMLUtils.createSingleNode("sender", sender.toXML()));
		}
		if (receiver != null) {
			buffer.append(XMLUtils.createSingleNode("receiver",
					receiver.toXML()));
		}
		buffer.append("</friendMessage>");
		return buffer.toString();
	}
}
